package com.ibm.productivity.tools.blogplugin.views;

import org.eclipse.core.runtime.Preferences;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.ibm.productivity.tools.blogplugin.Activator;

public class AccountInputDialog extends Dialog {

	String username;
	String password;

	public String getUsername() {
		return username;
	}

	public String getPassword() {
		return password;
	}

	/**
	 * @param parent
	 */
	public AccountInputDialog(Shell parent) {
		super(parent);
	}

	/**
	 * @param parent
	 * @param style
	 */
	public AccountInputDialog(Shell parent, int style) {
		super(parent, style);
	}

	/**
	 * Makes the dialog visible.
	 * 
	 * @return
	 */
	public void open() {
		Shell parent = getParent();
		final Shell shell = new Shell(parent, SWT.TITLE | SWT.BORDER
				| SWT.APPLICATION_MODAL);
		shell.setText("Account InputDialog");

		shell.setLayout(new GridLayout(3, true));
		GridData gridLayout1 = new GridData(100, SWT.DEFAULT | SWT.RIGHT);
		GridData gridLayout2 = new GridData(200, SWT.DEFAULT);		
		gridLayout2.horizontalSpan = 2;
		//gridLayout2.heightHint = 20;
		//gridLayout2.widthHint = 120;
		
		Label label = new Label(shell, SWT.NULL|SWT.RIGHT);
		label.setText("Google Account: ");
		label.setLayoutData(gridLayout1);
		final Text text = new Text(shell, SWT.SINGLE | SWT.BORDER);
		Label label2 = new Label(shell, SWT.NULL|SWT.RIGHT);
		label2.setText("Password: ");
		label2.setLayoutData(gridLayout1);
		final Text text2 = new Text(shell, SWT.BORDER | SWT.PASSWORD);
		text.setLayoutData(gridLayout2);
		text2.setLayoutData(gridLayout2);

		
		final Button buttonOK = new Button(shell, SWT.PUSH);
		buttonOK.setText("    Ok    ");
		buttonOK.setLayoutData(gridLayout1);
	
		final Button buttonCancel = new Button(shell, SWT.PUSH);
		buttonCancel.setText("    Cancel   ");
		buttonCancel.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
	
		
		text.addListener(SWT.Modify, new Listener() {
			public void handleEvent(Event event) {
				try {
					username = text.getText();
					password = text2.getText();
					if (username.length() != 0 && password.length() != 0)
						buttonOK.setEnabled(true);
					else
						buttonOK.setEnabled(false);
				} catch (Exception e) {
					buttonOK.setEnabled(false);
				}
			}
		});

		text2.addListener(SWT.Modify, new Listener() {
			public void handleEvent(Event event) {
				try {
					username = text.getText();
					password = text2.getText();
					if (username.length() != 0 && password.length() != 0)
						buttonOK.setEnabled(true);
					else
						buttonOK.setEnabled(false);
				} catch (Exception e) {
					buttonOK.setEnabled(false);
				}
			}
		});

		text2.addListener(SWT.KeyDown, new Listener() {
			public void handleEvent(Event event) {
				try {
					if (event.keyCode == SWT.CR)
						shell.dispose();
				} catch (Exception e) {
					buttonOK.setEnabled(false);
				}
			}
		});

		// button OK pushed
		buttonOK.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event event) {
				shell.dispose();
			}
		});

		// button Cancel pushed
		buttonCancel.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event event) {
				username = password = null;
				shell.dispose();
			}
		});

		shell.addListener(SWT.Traverse, new Listener() {
			public void handleEvent(Event event) {
				if (event.detail == SWT.TRAVERSE_ESCAPE)
					event.doit = false;
			}
		});

//		text.setText("tangqiaoboy@gmail.com");
//		text.setSelection(0, 11);
//		text2.setText("");		
//		username = password = null;
		
		// readdata from preferece page
		Preferences st = Activator.getDefault().getPluginPreferences();
		username = st.getString("Blog_User_Name");
		password = st.getString("Blog_Pass_Word");
		text.setText(st.getString("Blog_User_Name"));
		text2.setText(st.getString("Blog_Pass_Word"));
		
		shell.pack();
		shell.open();

		Display display = parent.getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}

	}

	public static void main(String[] args) {
		Shell shell = new Shell();
		AccountInputDialog dialog = new AccountInputDialog(shell);
		dialog.open();
		System.out.println(dialog.getUsername());
		System.out.println(dialog.getPassword());
	}
}
